package com.ruoyi.quartz.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.SysJobInfoConfig;
import com.ruoyi.quartz.service.ISysJobInfoConfigService;
import com.ruoyi.quartz.util.CronUtils;

/**
 * job配置Controller
 * 
 * @author ruoyi
 * @date 2023-05-01
 */
@Controller
@RequestMapping("/monitor/job")
public class SysJobInfoConfigController extends BaseController
{
    private String prefix = "monitor/job";

    @Autowired
    private ISysJobInfoConfigService sysJobInfoConfigService;

    @RequiresPermissions("monitor:job:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/job";
    }

    /**
     * 查询job配置列表
     */
    @RequiresPermissions("monitor:job:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysJobInfoConfig sysJobInfoConfig)
    {
        startPage();
        List<SysJobInfoConfig> list = sysJobInfoConfigService.selectSysJobInfoConfigList(sysJobInfoConfig);
        return getDataTable(list);
    }
    
    @RequiresPermissions("monitor:job:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("name", "job");
        mmap.put("job", sysJobInfoConfigService.selectSysJobInfoConfigById(id));
        return prefix + "/detail";
    }

    /**
     * 导出job配置列表
     */
    @RequiresPermissions("monitor:job:export")
    @Log(title = "job配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysJobInfoConfig sysJobInfoConfig)
    {
        List<SysJobInfoConfig> list = sysJobInfoConfigService.selectSysJobInfoConfigList(sysJobInfoConfig);
        ExcelUtil<SysJobInfoConfig> util = new ExcelUtil<SysJobInfoConfig>(SysJobInfoConfig.class);
        return util.exportExcel(list, "job配置数据");
    }

    /**
     * 新增job配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存job配置
     */
    @RequiresPermissions("monitor:job:add")
    @Log(title = "job配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysJobInfoConfig sysJobInfoConfig)
    {
        return toAjax(sysJobInfoConfigService.insertSysJobInfoConfig(sysJobInfoConfig));
    }

    /**
     * 修改job配置
     */
    @RequiresPermissions("monitor:job:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysJobInfoConfig sysJobInfoConfig = sysJobInfoConfigService.selectSysJobInfoConfigById(id);
        mmap.put("sysJobInfoConfig", sysJobInfoConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存job配置
     */
    @RequiresPermissions("monitor:job:edit")
    @Log(title = "job配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysJobInfoConfig sysJobInfoConfig)
    {
        return toAjax(sysJobInfoConfigService.updateSysJobInfoConfig(sysJobInfoConfig));
    }
    
    
    
    /**
     * 任务调度立即执行一次
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/run")
    @ResponseBody
    public AjaxResult run(SysJobInfoConfig job)
    {
        boolean result = sysJobInfoConfigService.run(job);
        return result ? success() : error("任务不存在或已过期！");
    }

    /**
     * 删除job配置
     */
    @RequiresPermissions("monitor:job:remove")
    @Log(title = "job配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysJobInfoConfigService.deleteSysJobInfoConfigByIds(ids));
    }
    
    /**
     * Cron表达式在线生成
     */
    @GetMapping("/cron")
    public String cron()
    {
        return prefix + "/cron";
    }
    /**
     * 查询cron表达式近5次的执行时间
     */
    @GetMapping("/queryCronExpression")
    @ResponseBody
    public AjaxResult queryCronExpression(@RequestParam(value = "cronExpression", required = false) String cronExpression)
    {
        if (sysJobInfoConfigService.checkCronExpressionIsValid(cronExpression))
        {
            List<String> dateList = CronUtils.getRecentTriggerTime(cronExpression);
            return success(dateList);
        }
        else
        {
            return error("表达式无效");
        }
    }
}
